package com.th.supcom.pds.impl.service.internal.phamapply.command;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import com.th.supcom.pds.domain.pojo.InpPhamApplyDetailSummary;
import com.th.supcom.pds.domain.pojo.PdsPatPhamRemain;
import com.th.supcom.pds.domain.vo.InpPhamDispenseDetailVO;
import com.th.supcom.pds.impl.common.enums.AvailableEnum;
import com.th.supcom.pds.impl.service.internal.phamapply.PhamDispenseConvertUtils;

/**
 * 更新患者剩余药品数量
 * 
 * @author leaqua@qq.com
 */
public class UpdateRemainPham extends AbstractPhamApplyManagerBean implements Command
{

    @Override
    public boolean execute (Context arg0) throws Exception
    {
        PhamDispenseContext context = (PhamDispenseContext) arg0;
        // 患者剩余药品数量
        Map <Long, Map <String, PdsPatPhamRemain>> patPhamRemainMap = context.getPatPhamRemainMap ();
        // 本次执行明细
        List <InpPhamDispenseDetailVO> dispenseList = context.getTotalDispenseList ();
        Map <Long, List <InpPhamDispenseDetailVO>> dispenseDetailMap = PhamDispenseConvertUtils.convertListToMapByPaivisitID (dispenseList);
        // 患者信息
        Map <String, InpPhamApplyDetailSummary> applySummaryMap = context.getPhamInfoMap ();
        // 需要更新的剩余药品明细
        List <PdsPatPhamRemain> updateRemainList = new ArrayList <PdsPatPhamRemain> ();
        // 需要调价的剩余药品数量
        List <PdsPatPhamRemain> addRemainList = new ArrayList <PdsPatPhamRemain> ();
        Set <Map.Entry <Long, List <InpPhamDispenseDetailVO>>> set = dispenseDetailMap.entrySet ();
        // 遍历所有患者
        for (Iterator <Map.Entry <Long, List <InpPhamDispenseDetailVO>>> it = set.iterator (); it.hasNext ();)
        {
            Map.Entry <Long, List <InpPhamDispenseDetailVO>> entry = (Map.Entry <Long, List <InpPhamDispenseDetailVO>>) it.next ();
            Map <String, PdsPatPhamRemain> patRemain = patPhamRemainMap.get (entry.getKey ());
            List <InpPhamDispenseDetailVO> patDispenseList = entry.getValue ();
            recursionSinglePatAllPham (applySummaryMap, updateRemainList, addRemainList, entry, patRemain, patDispenseList);
        }
        if (!CollectionUtils.isEmpty (addRemainList))
        {
            this.getInpPatPhamLeftBasicService ().addPdsPatPhamRemainList (addRemainList);
        }
        if (!CollectionUtils.isEmpty (updateRemainList))
        {
            this.getInpPatPhamLeftBasicService ().modifyPdsPatPhamRemainList (updateRemainList);
        }

        return false;
    }

    /**
     * 遍历单个患者的所有药品
     * 
     * @param applySummaryMap 单个患者药品汇总
     * @param updateRemainList 待更新剩余药品列表
     * @param addRemainList 待添加剩余药品列表
     * @param entry Map的键值对
     * @param patRemain 患者已有剩余药品信息
     * @param patDispenseList 单个患者所有请领明细
     */
    private void recursionSinglePatAllPham (Map <String, InpPhamApplyDetailSummary> applySummaryMap, List <PdsPatPhamRemain> updateRemainList,
                                            List <PdsPatPhamRemain> addRemainList, Map.Entry <Long, List <InpPhamDispenseDetailVO>> entry,
                                            Map <String, PdsPatPhamRemain> patRemain, List <InpPhamDispenseDetailVO> patDispenseList)
    {
        // 遍历单个患者所有药品
        for (InpPhamDispenseDetailVO inpPhamDispenseDetailVO : patDispenseList)
        {
            // 遍历单个患者单种药
            InpPhamApplyDetailSummary inpPhamApplyDetailSummary = applySummaryMap.get (inpPhamDispenseDetailVO.getPhamStdCode ());

            // 如果不是是大包装小剂量
            if (null == inpPhamApplyDetailSummary.getLargePackageSmallDosage ()
                || !AvailableEnum.YES.getEnumCode ().equals (inpPhamApplyDetailSummary.getLargePackageSmallDosage ()))
            {
                continue;
            }
            PdsPatPhamRemain phamRemain = null;
            // 如果患者之前有剩余药品数量
            if (patRemain != null)
            {
                phamRemain = patRemain.get (inpPhamDispenseDetailVO.getPhamStdCode ());
            }
            // 以剂量为单位本次摆药数量
            double currentDispenseAmount = inpPhamDispenseDetailVO.getDispenseAmount () * inpPhamDispenseDetailVO.getFactor () * inpPhamApplyDetailSummary.getDosagePerUnit ();
            // 本次医嘱执行的药品数量
            double applyDosage = inpPhamDispenseDetailVO.getDosage () * inpPhamDispenseDetailVO.getScheduleCount ();

            // 如果当前药品没有剩余数量
            if (null == phamRemain)
            {
                PdsPatPhamRemain addRemain = new PdsPatPhamRemain ();
                addRemain.setPaiVisitId (entry.getKey ());
                addRemain.setPhamStdCode (inpPhamDispenseDetailVO.getPhamStdCode ());
                addRemain.setPhamUnit (inpPhamDispenseDetailVO.getPhamUnit ());
                addRemain.setAmountLeft (currentDispenseAmount - applyDosage);
                addRemainList.add (addRemain);
            }
            else
            {
                phamRemain.setAmountLeft (phamRemain.getAmountLeft () + currentDispenseAmount - applyDosage);
                updateRemainList.add (phamRemain);
            }
        }
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (UpdateRemainPham.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, TH Supcom Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
}
